package com.cryptape.cita_wallet.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cryptape.cita_wallet.CytonApplication;
import com.cryptape.cita_wallet.bean.MyHMIOOrderItem;
import com.mangoworld.android.R;

import java.util.List;

/**
 * Created by dengming on 1/3/20.
 */

public class MyMinerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MyHMIOOrderItem> datas;
    private CloudMinerAdapter.OnItemClickListener onItemClickListener;

    public MyMinerAdapter(List<MyHMIOOrderItem> cloudMinerList){
        this.datas = cloudMinerList;
    }

    public void setOnItemClickListener(CloudMinerAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyMinerAdapter.MyMinerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_miner,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        MyMinerViewHolder clountMinerViewHolder = ((MyMinerViewHolder)holder);
        MyHMIOOrderItem bean = datas.get(position);
        clountMinerViewHolder.btn_miner_status.setText(bean.getStatusName());
        if(bean.getLevelName().equals("M1"))
        {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m1);
        }else if(bean.getLevelName().equals("M2")) {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m2);
        }else if(bean.getLevelName().equals("M3")) {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m3);
        }else if(bean.getLevelName().equals("M4")) {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m4);
        }
        clountMinerViewHolder.tv_miner_name.setText(bean.getLevelName()+ CytonApplication.getInstance().getString(R.string.cloud_miner));
        clountMinerViewHolder.tv_power.setText(bean.getCalPower()+"T");
        clountMinerViewHolder.tv_left.setText(bean.getSurplusMoney());
        clountMinerViewHolder.tv_miner_time.setText(bean.getCreateTime());
//        ((ClountMinerViewHolder)holder).btn_buy.setTag(new Integer(position));
    }

    @Override
    public int getItemCount() {
        return datas == null?0:datas.size();
    }

    class MyMinerViewHolder extends RecyclerView.ViewHolder {
        public Button btn_miner_status;
        ImageView miner_image;
        TextView tv_miner_name;
        TextView tv_power;
        TextView tv_miner_time;
        TextView tv_left;

        public MyMinerViewHolder(View view) {
            super(view);
            view.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, (int) view.getTag());
                }
            });
            btn_miner_status = view.findViewById(R.id.btn_miner_status);
            miner_image = view.findViewById(R.id.miner_image);
            tv_miner_name  = view.findViewById(R.id.tv_miner_name);
            tv_power  = view.findViewById(R.id.tv_power);
            tv_miner_time  = view.findViewById(R.id.tv_miner_time);
            tv_left  = view.findViewById(R.id.tv_left);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
